Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
 - Compress.c

Compress.c

Caricato da:
Scarica il programma completo

  1. /***************************************************************************
  2.  *   Copyright (C) 2008 by Lorenzo La Porta   *
  3.  *   lorelapo@gmail.com   *
  4.  *                                                                         *
  5.  *   This program is free software; you can redistribute it and/or modify  *
  6.  *   it under the terms of the GNU General Public License as published by  *
  7.  *   the Free Software Foundation; either version 2 of the License, or     *
  8.  *   (at your option) any later version.                                   *
  9.  *                                                                         *
  10.  *   This program is distributed in the hope that it will be useful,       *
  11.  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
  12.  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
  13.  *   GNU General Public License for more details.                          *
  14.  *                                                                         *
  15.  *   You should have received a copy of the GNU General Public License     *
  16.  *   along with this program; if not, write to the                         *
  17.  *   Free Software Foundation, Inc.,                                       *
  18.  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
  19.  ***************************************************************************/
  20.  
  21.  
  22. #include "Compress.h"
  23.  
  24. int compress(char *in, char *outn)
  25. {
  26.         int i, j, c, res;
  27.         init_lett();
  28.         if((res=ana_freq(in))<0)
  29.                 return -1;
  30.         create_template();
  31.         FILE *fp=fopen(in, "rb");
  32.         unlink(outn);
  33.         FILE *out=fopen(outn, "wb");
  34.         if(out==NULL)
  35.                 return -2;
  36.         bitstream_t *bs=bitstream_new(out, 0);
  37.         for(i=0;i<257;i++)
  38.         {
  39.                 for(j=0;j<6;j++)
  40.                         bitstream_add_bit(bs, GETNBIT(stream[i].n, j));
  41.                 for(j=0;j<stream[i].n;j++)
  42.                         bitstream_add_bit(bs, GETNBIT(stream[i].w, j));
  43.         }
  44.         while((c=fgetc(fp))!=EOF)
  45.         {
  46.                 for(i=0;i<stream[c&255].n;i++)
  47.                         bitstream_add_bit(bs, GETNBIT(stream[c&255].w, i));
  48.         }
  49.         for(i=0;i<stream[256].n;i++)
  50.                 bitstream_add_bit(bs, GETNBIT(stream[256].w, i));
  51.         bitstream_free(bs);
  52.         fclose(fp);
  53.         fclose(out);
  54.         free_redundtree(redundtree);
  55.         return 0;
  56. }
  57.  
  58. int decompress(char *in, char *outn)
  59. {
  60.         int i, j, c;
  61.         redundtree_t *decode;
  62.         FILE *fp=fopen(in, "rb");
  63.         if(fp==NULL)
  64.                 return -1;
  65.         unlink(outn);
  66.         FILE *out=fopen(outn, "wb");
  67.         if(out==NULL)
  68.                 return -2;
  69.         bitstream_t *bs=bitstream_new(fp, 1);
  70.         redundtree=redund_new();
  71.         decode=redundtree;
  72.         for(i=0;i<257;i++)
  73.         {
  74.                 stream[i].n=0;
  75.                 for(j=0;j<6;j++)
  76.                 {
  77.                         c=bitstream_read_bit(bs);
  78.                         SETNBIT(stream[i].n, j, c);
  79.                 }
  80.                 for(j=0;j<stream[i].n;j++)
  81.                 {
  82.                         if(bitstream_read_bit(bs))
  83.                         {
  84.                                 if(decode->dx==NULL)decode->dx=redund_new();
  85.                                 decode=decode->dx;
  86.                                
  87.                         }
  88.                         else
  89.                         {
  90.                                 if(decode->sx==NULL)decode->sx=redund_new();
  91.                                 decode=decode->sx;
  92.                         }
  93.                 }
  94.                 decode->c=i;
  95.                 decode=redundtree;
  96.         }
  97.         decode=redundtree;
  98.         while(1)
  99.         {
  100.                 c=bitstream_read_bit(bs);
  101.                 if(c)
  102.                         decode=decode->dx;
  103.                 else
  104.                         decode=decode->sx;
  105.                
  106.                 if(decode->sx==NULL&&decode->dx==NULL)
  107.                 {
  108.                         if(decode->c==256)break;
  109.                         fputc(decode->c, out);
  110.                         decode=redundtree;
  111.                 }
  112.         }
  113.         bitstream_free(bs);
  114.         fclose(out);
  115.         fclose(fp);
  116.         free_redundtree(redundtree);
  117.         return 0;
  118. }